<?php


namespace app\api\controller;

use app\api\model\MemberInfoModel;
use app\api\model\MemberModel;
use service\UploadService as Upload;
use think\File;
use think\Request;
use \think\Db;
/**
 * @title   积分模块
 * @desc    积分及佣金操作模块
 * Class Credit
 * @package app\api\controller
 */
class Credit extends AuthController {

    /**
     * @title 积分赠送
     * @desc 积分赠送功能
     * @param string $account - 被转让方的账号
     */
    public function transfer(Request $request) {
        //@todo 互转详细代码
        //登录态用户信息
        $info = $this->_userInfo;
        //获取转赠用户的账号
        $account = trim($request->get('account'));
        //获取转赠用户的积分
        $credit1 = trim($request->get('credit1'));

        //转赠用户信息
        $people_info = Db::name('meetyou_member')->where(['account'=> $account])->find();
        if(empty($people_info)){
            $this->error('转赠用户不存在！');
        }

        //获取用户信息
        $member = Db::name('meetyou_member')->where(['id' => $info['id']])->find();
        $daa = $member['credit1'];
        $member_credit1 = $daa - $credit1;
        if($member_credit1 < 0 ){
            $this->error('转赠用户积分不足！');
        }
        $update_info = Db::name('meetyou_member')->where(['id' => $info['id']])->update([
            'credit1' => $member_credit1,
        ]);
        if(empty($update_info)){
            $this->error('扣除转赠用户积分不成功');
        }

        $da = $people_info['credit1'];
        $people_credit1 = $da + $credit1;
        //更新转赠用户积分
        $update = Db::name('meetyou_member')->where(['account'=> $account ])->update([
            'credit1' =>  $people_credit1,
        ]);
        if(!empty($update)){
            $this->success('积分赠送成功'
            );

        }



    }

    /**
     * @title 余额转积分
     * @desc 余额转积分功能
     * @param float $num - 余额数
     */
    public function toCredit1(Request $request) {
        //@todo 余额转积分
        //登录态用户信息
         $info = $this->_userInfo;
        //获取余额
        $num = trim($request->get('num'));

        //获取用户信息
        $member = Db::name('meetyou_member')->where(['id'=> $info['id']])->find();
        $sale = $member['credit2'];
        $credit2 = $sale - $num;
        if($credit2 < 0 ){
            $this->error('余额转积分不足！');
        }

        $member_info = Db::name('meetyou_member')->where(['id'=> $info['id']])->update([
            'credit2' =>  $credit2,
        ]);
        if(empty($member_info)){
            $this->error('余额扣除不成功');
        }

        $da = $member['credit1'];
        $credit1 = $da + $num;
        $update = Db::name('meetyou_member')->where(['id'=> $info['id']])->update([
            'credit1' =>  $credit1,
        ]);
        if(!empty($update)){

            $this->success('余额转积分成功！');
            }

    }


}